package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Transactional
    @Override
    public void defaultAddress(AddressBook addressBook) {
        //1.取消当前的所有的默认地址 is_default 改成 0
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
        addressBookLambdaUpdateWrapper.eq(AddressBook::getUserId, BaseContext.get());
        super.update(addressBookLambdaUpdateWrapper);//update addressBook set is_default = 0 where user_id=?
        //2.默认地址的设置
        addressBook.setIsDefault(1);
        super.updateById(addressBook);//update addressBook set is_default = 1 where id = ?
    }
}
